package cn.swufe.edu.stu.hw_tools.Cal;

import static cn.swufe.edu.stu.hw_tools.StringToNum2.calculateHex;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.swufe.edu.stu.hw_tools.R;

public class CalCoderActivity extends AppCompatActivity {
    private Button backbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cal_coder);

        backbtn = findViewById(R.id.calcoderbackbutton);

        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(2,intent);
                finish();
            }
        });
    }

    public void input(View btn){
        TextView textView = findViewById(R.id.textViews1);
        TextView v = findViewById(R.id.textViews2);
        String str = textView.getText().toString();
        String add = getResources().getResourceEntryName(btn.getId());
        boolean hasCal = false;
        if(add.equals("buttons11") || add.equals("buttons21") || add.equals("buttons31")
                || add.equals("buttons41") || add.equals("buttons51") || add.equals("buttons61")
                || add.equals("buttons22") || add.equals("buttons23") || add.equals("buttons25")
                || add.equals("buttons32") || add.equals("buttons33") || add.equals("buttons34") || add.equals("buttons35")
                || add.equals("buttons42") || add.equals("buttons43") || add.equals("buttons44") || add.equals("buttons45")
                || add.equals("buttons52") || add.equals("buttons53") || add.equals("buttons54") || add.equals("buttons55")
                || add.equals("buttons63") ){
            if(str.equals("0")){
                str = "";
            }
            Button button = findViewById(btn.getId());
            str += button.getText().toString();
            textView.setText(str);
        }else if(add.equals("buttons65")){
            try {
                str = str.replaceAll("x", "*");
                long ans = calculateHex(str);
                String sans = Long.toHexString(ans);
                v.setText(sans);
                hasCal = true;
            } catch (Exception e) {
                textView.setText("");
                v.setText("");
                Toast.makeText(this, "(#｀ε´# )ゞ请输入合法的表达式", Toast.LENGTH_SHORT).show();
            }
        }else if(add.equals("buttons12")){
            String str2 = str;
            str2 = str2.replaceAll("x", "*");
            long ans = calculateHex(str2);
            ans *= 2;
            String sans = Long.toHexString(ans);
            textView.setText(sans);
            v.setText("");
        }else if(add.equals("buttons13")){
            String str2 = str;
            str2 = str2.replaceAll("x", "*");
            long ans = calculateHex(str2);
            ans /= 2;
            String sans = Long.toHexString(ans);
            textView.setText(sans);
            v.setText("");
        }else if(add.equals("buttons14")){
            textView.setText("");
            v.setText("");
        }else if(add.equals("buttons15")){
            try {
                str = str.substring(0,str.length()-1);
                textView.setText(str);
            } catch (Exception e) {
                Toast.makeText(this, "(#ﾟﾛﾟ#)！已经空了，别点了......", Toast.LENGTH_SHORT).show();
            }
        }else if(add.equals("buttons62")){
            try {
                String str2 = str;
                str2 = str2.replaceAll("x", "*");
                long ans = calculateHex(str2);
                ans = -ans;
                String sans = Long.toHexString(ans);
                textView.setText(sans);
                v.setText("");
            } catch (Exception e) {
                textView.setText("");
                v.setText("");
                Toast.makeText(this, "(#｀ε´# )ゞ请输入合法的表达式", Toast.LENGTH_SHORT).show();
            }
        }else if(add.equals("buttons24") || add.equals("buttons64")){
            Toast.makeText(this, "(´ω｀*)该按件暂不可用......", Toast.LENGTH_SHORT).show();
        }
        try {
            String ans2 = textView.getText().toString();
            ans2 = ans2.replaceAll("a","A")
                    .replaceAll("b","B")
                    .replaceAll("c","C")
                    .replaceAll("d","D")
                    .replaceAll("e","E")
                    .replaceAll("f","F");
            textView.setText(ans2);
        }catch(Exception e){
            textView.setText("");
        }
        try {
            String ans2 = v.getText().toString();
            ans2 = ans2.replaceAll("a","A")
                    .replaceAll("b","B")
                    .replaceAll("c","C")
                    .replaceAll("d","D")
                    .replaceAll("e","E")
                    .replaceAll("f","F");
            v.setText(ans2);
        }catch(Exception e){
            v.setText("");
        }
        if(hasCal){
            textView.setText("");
        }
        //calculateHex

    }

}